package com.androgl.game;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

public class TextureMgr {
	
	private static TextureMgr ins;
	int[] textureIDs = new int[1];
	
	public TextureMgr() {
		
		
	}
	
	public TextureMgr getInstance() {
		if (ins == null)
			ins = new TextureMgr();
		
		return ins;
	}
	
	// Load an image into GL texture
	public void loadTexture(GL10 gl, Context context) {
	      gl.glGenTextures(1, textureIDs, 0); // Generate texture-ID array

	      gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);   // Bind to texture ID
	      // Set up texture filters
	      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
	      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
	  
	      // Construct an input stream to texture image "res\drawable\icon.png"
	      InputStream istream = context.getResources().openRawResource(android.R.drawable.icon);	//[TODO] How to use R =? Resource
	      Bitmap bitmap;
	      try {
	         // Read and decode input as bitmap
	         bitmap = BitmapFactory.decodeStream(istream);
	      } finally {
	         try {
	            istream.close();
	         } catch(IOException e) { }
	      }
	  
	      // Build Texture from loaded bitmap for the currently-bind texture ID
	      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
	      bitmap.recycle();
	   }
}
